const Koa = require('koa');
const cors = require('koa2-cors');
const route = require('koa-route');
const bodyParser = require('koa-bodyparser');
const app = new Koa();
app.use(bodyParser());
app.use(cors())
const news = require('./db/model/news');
const category = require('./db/model/category');
const school = require('./db/model/school');

const user = require('./db/model/user');
const tags = require('./db/model/tags');
const { password } = require('./config/db');

const userList = async ctx =>{
    ctx.response.type = 'json';
    let da = [];
    await user.findAll().then(data=>{
        da = data;
    });
    ctx.body = da;
};
const userCreate = async ctx =>{
    let body = ctx.request.body;
    user.create({
        'username':body['username'],
        'password':body['password'],
        'lastname':body['lastname'],
        'firstname':body['firstname'],
        'email':body['email'],
        'isadmin':1
    });
    ctx.body = 'success';
};
const userUpdate = async ctx =>{
    let body = ctx.request.body;
    const anews = await user.update({
        'username':body['username'],
        'password':body['password'],
        'lastname':body['lastname'],
        'firstname':body['firstname'],
        'email':body['email']
    },{
        where:{
            'id':body['id']
        }
    });
    ctx.body = 'success';
};
const userDelete = async ctx =>{
    let body = ctx.request.body;
    const anews = await user.destroy({
        where:{
            id:body['id']
        }
    });
    ctx.body = 'success';
};
const userLogin = async ctx =>{
    let body = ctx.request.body;
    let password = body['password'];
    let username = body['username'];
    const auser = await user.findOne({where:{username:username, password:password}});
    if(auser){
        ctx.body = 'success';
    }else{
        ctx.body = 'username or password is wrong';
    }
};

const tagsList = async ctx =>{
    ctx.response.type = 'json';
    let da = [];
    await tags.findAll().then(data=>{
        da = data;
    });
    ctx.body = da;
};
const tagsCreate = async ctx =>{
    let body = ctx.request.body;
    tags.create({
        'name':body['name'],
        'desc':body['desc']
    });
    ctx.body = 'success';
};
const tagsUpdate = async ctx =>{
    let body = ctx.request.body;
    const tags = await user.update({
        'name':body['name'],
        'desc':body['desc']
    },{
        where:{
            'id':body['id']
        }
    });
    ctx.body = 'success';
};
const tagsDelete = async ctx =>{
    let body = ctx.request.body;
    const tags = await user.destroy({
        where:{
            id:body['id']
        }
    });
    ctx.body = 'success';
};

/**
 * 查看所有新闻
 * @param {} ctx
 */
const newsList = async ctx =>{
    ctx.response.type = 'json';
    let da = [];
    await news.findAll().then(data=>{
        da = data;
    });
    ctx.body = da;
};

/**
 * 新建新闻
 * @param {} ctx
 */
const newsCreate = async ctx =>{
    let body = ctx.request.body;
    console.log(body['title'],body['content']);
    news.create({
        'title':body['title'],
        'content':body['content'],
        'create_by':body['create_by'],
        'category':body['category']
    });
    ctx.body = 'success';
}

/**
 * 更新新闻
 * @param {} ctx
 */
const newsUpdate = async ctx =>{
    let body = ctx.request.body;
    const anews = await news.update({
        'title':body['title'],
        'content':body['content'],
        'create_by':body['create_by'],
        'category':body['category']
    },{
        where:{
            'id':body['id']
        }
    });
    ctx.body = 'success';
}

/**
 * 删除新闻
 * @param {*} ctx
 */
const newsDelete = async ctx =>{
    let body = ctx.request.body;
    const anews = await news.destroy({
        where:{
            id:body['id']
        }
    });
    ctx.body = 'success';
}

/**
 * 查看所有分类
 * @param {*} ctx
 */
 const categoryList = async ctx =>{
    ctx.response.type = 'json';
    let da = [];
    await category.findAll().then(data=>{
        da = data;
    });
    ctx.body = da;
};

/**
 * 新建分类
 * @param {*} ctx
 */
const categoryCreate = async ctx =>{
    let body = ctx.request.body;
    category.create({
        'name':body['name'],
        'create_by':body['create_by']
    });
    ctx.body = 'success';
}
/**
 * 更新分类
 * @param {}} ctx
 */
const categoryUpdate = async ctx =>{
    let body = ctx.request.body;
    const anews = await category.update({
        'name':body['name'],
        'create_by':body['create_by']
    },{
        where:{
            'id':body['id']
        }
    });
    ctx.body = 'success';
}

/**
 * 删除分类
 * @param {*} ctx
 */
const categoryDelete = async ctx =>{
    let body = ctx.request.body;
    const anews = await category.destroy({
        where:{
            id:body['id']
        }
    });
    ctx.body = 'success';
}

const schoolList = async ctx =>{
    ctx.response.type = 'json';
    let da = [];
    await school.findAll().then(data=>{
        da = data;
    });
    ctx.body = da;
};

const schoolCreate = async ctx =>{
    let body = ctx.request.body;
    school.create({
        'name':body['name'],
        'female':body['female'],
        'male':body['male']
    });
    ctx.body = 'success';
};

const schoolUpate = async ctx =>{
    let body = ctx.request.body;
    const anews = await school.update({
        'name':body['name'],
        'female':body['female'],
        'male':body['male']
    },{
        where:{
            'id':body['id']
        }
    });
    ctx.body = 'success';
};

const schoolDelete = async ctx =>{
    let body = ctx.request.body;
    const anews = await school.destroy({
        where:{
            id:body['id']
        }
    });
    ctx.body = 'success';
};

/**
 * URL 关联
 */
app.use(route.get('/news/list', newsList));
app.use(route.post('/news/create', newsCreate));
app.use(route.post('/news/update', newsUpdate));
app.use(route.post('/news/delete', newsDelete));
app.use(route.get('/category/list', categoryList));
app.use(route.post('/category/create', categoryCreate));
app.use(route.post('/category/update', categoryUpdate));
app.use(route.post('/category/delete', categoryDelete));

app.use(route.get('/school/list', schoolList));
app.use(route.post('/school/create', schoolCreate));
app.use(route.post('/school/update', schoolUpate));
app.use(route.post('/school/delete', schoolDelete));

app.use(route.get('/user/list', userList));
app.use(route.post('/user/create', userCreate));
app.use(route.post('/user/update', userUpdate));
app.use(route.post('/user/delete', userDelete));
app.use(route.post('/user/login', userLogin));

app.use(route.get('/tags/list', tagsList));
app.use(route.post('/tags/create', tagsCreate));
app.use(route.post('/tags/update', tagsUpdate));
app.use(route.post('/tags/delete', tagsDelete));

//监听端口
app.listen(3001);